/**
 * Created by Administrator on 2017/9/16 0016.
 */
$(document).ready(function () {
    var dh = $(this).outerHeight();
    $(".main").height(dh*0.9+"px");
    $(".footer").height(dh*0.1+"px");

    var $userName = $("input[name=userName]");
    var $password = $("input[name=password]");
    var $yzm = $("input[name=imgYzm]");

    var $login_form = $("#login-form");
    var $msg = $("#msg");
    var $u_clear = $(".u-clear");



    $login_form.on('submit',function(e){
        e.preventDefault();
        var userName = $userName.val();
        var password = $password.val();
        var yzm = $yzm.val();

        if(! userName){
            $msg.text("请输入用户名");
            $msg.parent().removeClass("hide");
            return false
        } else if(! password){
            $msg.text("请输入密码");
            $msg.parent().removeClass("hide");
            return false
        }else if(!yzm){
            $msg.text("请输入验证码");
            $msg.parent().removeClass("hide");
            return false
        }
            var serialize = $("#login-form").serialize();
            $.ajax({
                url:'/login/check',
                type:'post',
                dataType:'json',
                data:serialize,
                success: function(msg){
                    if(msg.code == 1){
                        location.href = "/"
                    } else {
                        $msg.text(msg.message);
                        $msg.parent().removeClass("hide"); //删除 hide 样式表
                        sxYzm()
                    }
                },
                error: function(){
                    console.log("登陆异常");
                }
            })

        /*this.submit()*/
    })

    $userName.on('input',function(){
        var $this = $(this);
        if($this.val()){
            $this.next().removeClass("hide");
            $msg.parent().addClass("hide");

           /* if($msg.text()== "请输入用户名" || $msg.text()=="用户名不存在"){
                $msg.parent().addClass("hide");
            }*/
        }else{
            $this.next().addClass("hide");
        }
    })

    $password.on('input',function(){
        var $this = $(this);
        if($this.val()){
            $this.next().removeClass("hide");
            if($msg.text()== "请输入密码"){
                $msg.parent().addClass("hide");
            }
        }else{
            $this.next().addClass("hide");
        }
    })

    $yzm.on('input',function(){
        var $this = $(this);
        if($this.val()){
            if($msg.text()== "请输入验证码"||$msg.text()== "验证码输入错误或已过期"){
                $msg.parent().addClass("hide");
            }
        }
    })

   /* $userName.on('blur',function () {
        var userName = $userName.val()
        if(null != userName && userName != ""){
            $.ajax({
                url:'/login/findUser',
                type:'post',
                dataType:'json',
                data:{userName:userName},
                success:function (msg) {
                    if(msg.code == 0){
                        $msg.text(msg.message);
                        $msg.parent().removeClass("hide");
                    }

                }
            })
        }
    })*/

    for(var i =0 ;i<$u_clear.length;i++){
        $u_clear.on('click',function () {
            var $this = $(this);
            $this.prev().val("");
            $this.addClass("hide")
        })
    };


    var $chImage = $(".chImage");
    /*$chImage.on('click',function(){
        $(this).attr("src","/kaptcha.jpg?t=" + new Date().getTime());
    })*/

    $chImage.on('click',sxYzm)
    function sxYzm(){
        $chImage.attr("src","/kaptcha.jpg?t=" + new Date().getTime());
    }

})